//
//  SelectInfosV.swift
//  Employee
//
//  Created by swordjoy on 2017/3/27.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class SelectInfosV: UIView {
  enum SelectInfoType {
    case orderType
    case priority
    case rule
  }
  
  var selectInfoClick: ( (SelectInfoType) -> () )?
  
  fileprivate var subV: [SelectInfoV] = []
  
  fileprivate var titles: [String] = []
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    
    titles = ["工单分类", "优先级", "派单规则"]
    
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

extension SelectInfosV {
  
  func updateDetail(_ index: Int, detail: String) {
    
    self.subV[index].detail = detail
    
  }
  
}

// MARK: - action

extension SelectInfosV {
  @objc fileprivate func selectViewsAction(_ tap: UITapGestureRecognizer) {
    if let view = tap.view as? SelectInfoV {
      guard let closure = selectInfoClick else { return }
      
      switch view.tag {
      case 0:
        closure(.orderType)
      case 1:
        closure(.priority)
      case 2:
        closure(.rule)
      default:
        fatalError()
      }
    }
  }
}

// MARK: - fileprivate

extension SelectInfosV {
  fileprivate func create() {
    let margin: CGFloat = 10
    let height: CGFloat = 50
    
    for index in 0..<3 {
      let y = margin * ( index + 1 ).cgFloat + height * index.cgFloat
      let frame = CGRect(x: 0, y: y, width: UIScreen.width, height: height)
      let view = SelectInfoV(frame: frame, title: titles[index])
      let gesture = UITapGestureRecognizer(target: self, action: #selector(selectViewsAction(_:)))
      view.tag = index
      view.addGestureRecognizer(gesture)
      addSubview(view)
      subV.append(view)
    }
  }
}
